import Vue from "vue";
import VueRouter from "vue-router";
import Layout from "@/views/layout/index.vue";
import Login from "@/views/login/index.vue";
import Welcome from "@/views/welcome/index.vue";
import Users from "@/views/users/index.vue";
import Roles from "@/views/roles/index.vue";
import Rights from "@/views/rights/index.vue";
import Goods from "@/views/goods/index.vue";
import Params from "@/views/params/index.vue";
import Categories from "@/views/categories/index.vue";
import Orders from "@/views/orders/index.vue";
import Reports from "@/views/reports/index.vue";
import store from "@/store";
import Add from "@/views/goods/components/add.vue";

Vue.use(VueRouter);

const routes = [
  { path: "/login", component: Login },
  {
    path: "/",
    component: Layout,
    redirect: "/welcome",
    children: [
      { path: "welcome", component: Welcome },
      { path: "users", component: Users },
      { path: "roles", component: Roles },
      { path: "rights", component: Rights },
      { path: "goods", component: Goods },
      { path: "params", component: Params },
      { path: "categories", component: Categories },
      { path: "orders", component: Orders },
      { path: "reports", component: Reports },
      { path: "goods/add", component: Add },
    ],
  },
];

const router = new VueRouter({
  routes,
});

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = store.state.user.token;
  if (!token && to.path !== "/login") {
    next("/login");
  }
  next();
});
export default router;
